package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * ClassName: ApBehaviorEntryMapper
 * Author:   WGG
 * Date:    2022-12-2022/12/1-10:09
 * Version: 1.0
 * Description:
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult forwardBehavior(FollowBehaviorDto followBehaviorDto) {
        //判空
        if (followBehaviorDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.showBehavior(followBehaviorDto.getUserId(), null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
        //文章的id  需要前端进行传递
        apFollowBehavior.setArticleId(followBehaviorDto.getArticleId());
        //当前时间
        apFollowBehavior.setCreatedTime(new Date());
        //行为实体的id (就是ap_behavior_entry表中的id)

        apFollowBehavior.setEntryId(apBehaviorEntry.getId());
        //作者的id
        apFollowBehavior.setFollowId(followBehaviorDto.getFollowId());
        save(apFollowBehavior);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ApFollowBehavior showFollowBehavior(ArticleInfoDto dto) {
        if (dto != null){
            return getOne(Wrappers.<ApFollowBehavior>lambdaQuery().eq(ApFollowBehavior::getArticleId,dto.getArticleId()).eq(ApFollowBehavior::getEntryId,dto.getId()));
        }
        return null;
    }
}
